#include "TitleBar.h"
#include <QDebug>

#define BUTTON_HEIGHT 27
#define BUTTON_WIDTH 27
#define TITLE_HEIGHT 27

TitleBar::TitleBar(QWidget *parent)
	: QWidget(parent), m_isPressed(false),
	m_buttonType(MIN_MAX_BUTTON)
{
	initControl();
	initConnections();
	loadStyleSheet("Title");//加载样式
}

void TitleBar::initControl()
{
	m_pTitleIcon = new QLabel(this);
	m_pTitleContent = new QLabel(this);

	m_pButtonMin = new QPushButton(this);
	m_pButtonMax = new QPushButton(this);
	m_pButtonClose = new QPushButton(this);
	m_pButtonRestore = new QPushButton(this);

	m_pTitleContent->setFixedHeight(BUTTON_HEIGHT);
	m_pTitleIcon->setFixedHeight(BUTTON_HEIGHT);

	m_pButtonClose->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
	m_pButtonMin->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
	m_pButtonMax->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));
	m_pButtonRestore->setFixedSize(QSize(BUTTON_WIDTH, BUTTON_HEIGHT));

	
	m_pTitleIcon->setObjectName("TitleIcon");
	m_pTitleContent->setObjectName("TitleContent");

	m_pButtonMin->setObjectName("ButtonMin");
	m_pButtonMax->setObjectName("ButtonMax");
	m_pButtonClose->setObjectName("ButtonClose");
	m_pButtonRestore->setObjectName("ButtonRestore");

	QHBoxLayout *mylayout = new QHBoxLayout(this);
	mylayout->addWidget(m_pTitleIcon);
	mylayout->addWidget(m_pTitleContent);
	mylayout->addWidget(m_pButtonMin);
	mylayout->addWidget(m_pButtonMax);
	mylayout->addWidget(m_pButtonRestore);
	mylayout->addWidget(m_pButtonClose);

	mylayout->setContentsMargins(5, 0, 0, 0);
	mylayout->setSpacing(0);

	m_pTitleContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	setFixedHeight(BUTTON_HEIGHT);
	setWindowFlag(Qt::FramelessWindowHint);

}

void TitleBar::initConnections()
{
	connect(m_pButtonClose,SIGNAL(clicked()),this,SLOT(onButtonCloseClicked()));
	connect(m_pButtonMin, SIGNAL(clicked()), this, SLOT(onButtonMinClicked()));
	connect(m_pButtonMax, SIGNAL(clicked()), this, SLOT(onButtonMaxClicked()));
	connect(m_pButtonRestore, SIGNAL(clicked()), this, SLOT(onButtonRestoreClicked()));
}

void TitleBar::setTitleIcon(const QString &filePath)
{
	QPixmap icon(filePath);
	m_pTitleIcon->setFixedSize(icon.size());
	m_pTitleIcon->setPixmap(icon);

}
void TitleBar::setTitleContent(const QString &titleContent)
{
	m_pTitleContent->setText(titleContent);
	m_titleContent = titleContent;

}

void TitleBar::setTitleWidth(int width)
{
	setFixedWidth(width);
}

void TitleBar::setButtonType(ButtonType bottonType)
{
	m_buttonType = bottonType;

	switch (m_buttonType) {
	case MIN_BUTTON:
		m_pButtonClose->setVisible(true);
		m_pButtonMax->setVisible(false);
		m_pButtonRestore->setVisible(false);
		m_pButtonMin->setVisible(true);
		break;
	case MIN_MAX_BUTTON:
		m_pButtonClose->setVisible(true);
		m_pButtonMax->setVisible(true);
		m_pButtonRestore->setVisible(false);
		m_pButtonMin->setVisible(true);
		break;
	case ONLY_CLOSE_BUTTON:
		m_pButtonClose->setVisible(true);
		m_pButtonMax->setVisible(false);
		m_pButtonRestore->setVisible(false);
		m_pButtonMin->setVisible(false);
		break;
	};
}

void TitleBar::paintEvent(QPaintEvent *event)
{
	if (width() != parentWidget()->width()) {
		setFixedWidth(parentWidget()->width());
	}

	QPainter QPainter(this);
	QPainterPath pathBack;
	pathBack.setFillRule(Qt::WindingFill);
	pathBack.addRoundedRect(QRect(0, 0, width(), height()), 3, 3);

	QPainter.setRenderHint(QPainter::SmoothPixmapTransform, true);

	return QWidget::paintEvent(event);
}

void TitleBar::saveRestoreInfo(const QPoint &point, const QSize &size)
{
	m_restorePos = point;
	m_restoreSize = size;

}

void TitleBar::getRestoreInfo(QPoint &point, QSize &size)
{
	point = m_restorePos;
	size = m_restoreSize;

}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
	if (m_buttonType == MIN_MAX_BUTTON) {
		if (m_pButtonMax->isVisible()) {
			onButtonRestoreClicked();
		}
		else {
			onButtonMaxClicked();
		}
	}

	return QWidget::mouseDoubleClickEvent(event);

}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
	
	if (m_buttonType == MIN_MAX_BUTTON && m_pButtonMax->isVisible() == false) {
		//窗口最大化不能移动
	}
	else {
		m_isPressed = true;
		m_startMovePos = event->globalPos();
	}

	////() << "TitleBar::mousePressEvent";
	return QWidget::mousePressEvent(event);
	
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
	if (m_isPressed) {
		QPoint widgetPos = parentWidget()->pos();
		QPoint movePoint = event->globalPos() - m_startMovePos;
		m_startMovePos = event->globalPos();
		parentWidget()->move(widgetPos + movePoint);
	}

	////() << "TitleBar::mouseMoveEvent";
	return QWidget::mouseMoveEvent(event);

}

void TitleBar::mouseReleaseEvent(QMouseEvent *event)
{
	//if (m_isPressed) {
	m_isPressed = false;
	//	m_startMovePos = event->globalPos();
	//}

	////() << "TitleBar::mouseReleaseEvent";
	return QWidget::mouseReleaseEvent(event);

}

void TitleBar::loadStyleSheet(const QString &sheetName)
{
	QFile file(":/Resources/QSS/" + sheetName + ".css");
	file.open(QIODevice::ReadOnly);
	if (file.isOpen()) {
		QString styleSheet = this->styleSheet();
		styleSheet += file.readAll().data();

		setStyleSheet(styleSheet);
	}
	file.close();
}

void TitleBar::onButtonCloseClicked()
{
	emit signalButtonCloseClicked();
}

void TitleBar::onButtonMaxClicked()
{
	m_pButtonMax->setVisible(false);
	m_pButtonRestore->setVisible(true);

	emit signalButtonMaxClicked();
}

void TitleBar::onButtonMinClicked()
{
	emit signalButtonMinClicked();
}

void TitleBar::onButtonRestoreClicked()
{
	m_pButtonMax->setVisible(true);
	m_pButtonRestore->setVisible(false);

	emit signalButtonRestoreClicked();
}

TitleBar::~TitleBar()
{

}
